package cn.miw.android.bdmp3;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import cn.miw.android.bdmp3.adapter.InfoInitor;
import cn.miw.android.bdmp3.adapter.MenuListener;
import cn.miw.android.bdmp3.model.BaiDuList;
import cn.miw.android.bdmp3.model.BaiDuListItem;
import cn.miw.android.bdmp3.model.Catlogs.Catlog;
import cn.miw.android.bdmp3.service.Util;
import cn.miw.android.listViewAdapter.ListViewAdapter;

public class TopActivity extends Base implements OnItemClickListener, OnClickListener {
	private List<BaiDuListItem> rankLists = new ArrayList<BaiDuListItem>();
	private ListViewAdapter adapter;
	private InfoInitor initor;
	private Catlog catlog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ranktop);
		initor = new InfoInitor(this);
		try {
			catlog = (Catlog) getIntent().getExtras().getSerializable("catlog");
			Init();
		} catch (Exception e) {
			Toast.makeText(getApplicationContext(), "获取榜单数据错误，请重新进入。", Toast.LENGTH_LONG).show();
		}
	}

	private void Init() {
		Dialog.setMessage("下载 " + catlog.getName() + " 榜单信息中...");
		((TextView) findViewById(R.id.title)).setText(catlog.getName());

		adapter = new ListViewAdapter(initor, rankLists,"");
		ListView lv = (ListView) findViewById(R.id.listViewMusic);
		lv.setAdapter(adapter);
		lv.setOnItemClickListener(this);

		ImageButton btnBack = (ImageButton) findViewById(R.id.btnBack);
		btnBack.setOnClickListener(this);
		btnBack.setVisibility(View.VISIBLE);
	}

	@Override
	public void BuildData(Void... params) {
		xml = Util.getWebContent(catlog.getTrueURL(), "gbk");
		BaiDuList baiDuList = Util.parseBaiDuList(xml);
		rankLists.clear();
		rankLists.addAll(baiDuList.getSonges());
	}

	@Override
	public void ShowData() {
		((TextView) findViewById(R.id.nums)).setText(rankLists.size() + "首");
		adapter.notifyDataSetChanged();
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (rankLists.size() == 0) {
			miwHandler();
		}
	}

	public void onItemClick(AdapterView<?> owner, View view, int position, long id) {
		Util.startDown(this, rankLists.get(position));
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnBack:
			finish();
			break;
		case R.id.btnMenu:
			openContextMenu(v);
			break;
		default:
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
		}
		return false;
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		// int m = ((AdapterContextMenuInfo) menuInfo).position;
		// 长按时用这个取得菜单所按条目的位置
		int m = (Integer) v.getTag();
		BaiDuListItem item = rankLists.get(m);
		String songName = item.getSinger() + "-" + item.getSong() + ".mp3";
		menu.setHeaderTitle(songName);

		menu.add(0, 10 * 100000 + m, 0, "下载mp3").setOnMenuItemClickListener(menuListener);
		menu.add(0, 11 * 100000 + m, 0, "试听").setOnMenuItemClickListener(menuListener);
		menu.add(0, 12 * 100000 + m, 0, "下载歌词").setOnMenuItemClickListener(menuListener);
		menu.add(0, 13 * 100000 + m, 0, "分享到新浪微博").setOnMenuItemClickListener(menuListener);
		menu.add(0, 14 * 100000 + m, 0, "分享到腾讯微博").setOnMenuItemClickListener(menuListener);
		
		super.onCreateContextMenu(menu, v, menuInfo);
	}
	private MenuListener menuListener = new MenuListener(this, rankLists);
}